package com.lvyq.myplayer.adapter.collect

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lvyq.myplayer.parcelable.SongInfoData
import com.lvyq.myplayer.widget.LoadNoMoreView
import com.lvyq.myplayer.widget.common.collect.CollectItemTopView
import com.lvyq.myplayer.widget.common.song.SongItemPlainTextView

/**
 * 文件描述：PlayCollectAdapter
 * 作者：lvyq
 * 创建时间：2025-06-20
 * 修改时间：2025-06-20
 */
class PlayCollectAdapter : RecyclerView.Adapter<PlayCollectAdapter.PlayCollectViewHolder>(){

    companion object{
        val LOAD_TOP = 0 //初始化
        val LOAD_INIT = 1 //加载更多
        val LOAD_END = 2
    }


    interface OnItemClickListener {
        fun onItemClick(data: SongInfoData, position: Int)
    }

    private var onItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener
    }




    var listData=ArrayList<SongInfoData>()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlayCollectViewHolder {
        if (viewType== LOAD_TOP){
            return PlayCollectViewHolder(CollectItemTopView(parent.context))
        }
        if (viewType== LOAD_END){
            return PlayCollectViewHolder(LoadNoMoreView(parent.context))
        } else{
            return PlayCollectViewHolder(SongItemPlainTextView(parent.context))
        }
    }

    override fun getItemCount(): Int {
        return listData.size+2
    }

    override fun getItemViewType(position: Int): Int {

        //第一个条目
        if (position==0)  return LOAD_TOP
        //最后一个条目
        if (position == listData.size+1) return LOAD_END
        return LOAD_INIT

    }

    override fun onBindViewHolder(holder: PlayCollectViewHolder, position: Int) {
        //第一个条目
        if (getItemViewType(position)== LOAD_TOP){
            val item = holder.itemView as CollectItemTopView
            item.setData(listData)
        }
        //普通条目
        if (getItemViewType(position)== LOAD_INIT ){
            val data = listData[position-1]
            val item = holder.itemView as SongItemPlainTextView
            item.setData(data)
            item.setOnClickListener{
                onItemClickListener?.onItemClick(data,position-1)
            }
        }else{
            return
        }

    }

    fun initData(lst: ArrayList<SongInfoData>) {
        listData.clear()
        listData=lst
        notifyDataSetChanged()
    }


    inner class PlayCollectViewHolder(view:View): RecyclerView.ViewHolder(view) {
    }


}